package com.springboot.wisdom.system.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.springboot.wisdom.common.common.ResultUtil;
import com.springboot.wisdom.common.common.bean.DeleteDTO;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.bean.PageQueryDTO;
import com.springboot.wisdom.serviceApi.system.model.dto.RoleDTO;
import com.springboot.wisdom.serviceApi.system.model.entity.RoleDO;
import com.springboot.wisdom.system.mapeer.RoleMapper;
import com.springboot.wisdom.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl extends MPJBaseServiceImpl<RoleMapper, RoleDO> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public MyResult<List<?>> list(PageQueryDTO pageQueryDTO, RoleDTO roleDTO) {
        List<RoleDTO> roleDOS = roleMapper.selectJoinList(RoleDTO.class, new MPJLambdaWrapper<RoleDO>()
//                .selectAll(RoleDO.class)
                        .select(RoleDO::getRoleId, RoleDO::getRoleName, RoleDO::getPermissions, RoleDO::getMenuPermissions)
                        .select(RoleDO::getRemark, RoleDO::getCreateBy, RoleDO::getCreateTime, RoleDO::getUpdateTime)
                        .selectAs(RoleDO::getRoleType, RoleDTO::getRoleTypeString)
        );

        return ResultUtil.success(roleDOS);


//        List<RoleDO> roleDOS = roleMapper.selectList(null);
//        List<MenuTree> menuTrees = roleMapper.selectJoinList(MenuTree.class, new MPJLambdaWrapper<MenuDO>()
//                .selectAll(MenuDO.class)
//                .selectCollection(MenuTree::getChildren, map1 -> {
//                    map1.all(subTable1, MenuDO.class);
//                    map1.result(MenuDO::getMenuName);
//                    map1.collection(MenuTree::getChildren, map2 -> {
//                        map2.all(subTable2, MenuDO.class);
//                        map1.result("t", MenuDO::getMenuName, MenuTree::getParentName);
//                        map2.result(subTable1, MenuDO::getMenuName, MenuTree::getParentName);
//                        return map2;
//                    });
//                    return map1;
//                })
//                .leftJoin(MenuDO.class, subTable1, MenuDO::getParentId, MenuDO::getMenuId)
//                .leftJoin(MenuDO.class, subTable2, MenuDO::getParentId, subTable1, MenuDO::getMenuId)
//                // 查询名称。
//                .eqIfExists(subTable1, MenuDO::getMenuName, menuDTO.getMenuName())
//                .eq(MenuDO::getParentId, StringUtil.isEmpty(menuDTO.getParentId()) ? "0" : menuDTO.getParentId())
//        );
//        return ResultUtil.success(menuTrees);


//        roleDOS.forEach(item -> {
//            System.out.println("roleDOS ----- item = " + item);
//        });
//
//        return ResultUtil.success(roleDOS);
    }

    @Override
    public MyResult<String> add(RoleDTO dto) {
        int result = roleMapper.insert(new RoleDO(null, dto.getRoleName(), dto.getRoleType()
                , dto.getPermissions(), dto.getRemark(), dto.getMenuPermissions()
                , "更新人员", null, DateTime.now()));
        return result != 0 ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }

    @Override
    public MyResult<String> edit(RoleDTO dto) {
        RoleDO entity = new RoleDO();
        entity.setRoleId(dto.getRoleId());
        entity.setRoleName(dto.getRoleName());
        entity.setRoleType(dto.getRoleType());
        entity.setPermissions(dto.getPermissions());
        entity.setRemark(dto.getRemark());
        entity.setMenuPermissions(dto.getMenuPermissions());
        entity.setCreateBy("更新人");
        entity.setUpdateTime(DateTime.now());

        int result = roleMapper.updateById(entity);
        return result > 0 ? ResultUtil.success("编辑成功") : ResultUtil.error("编辑失败");
    }

    @Override
    public MyResult<String> delete(String id) {
        int result = roleMapper.deleteById(id);
        return result > 0 ? ResultUtil.success("删除成功") : ResultUtil.error("删除失败");
    }

    @Override
    public MyResult<String> deleteList(DeleteDTO<String> dto) {
        int result = roleMapper.deleteByIds(dto.getIdList());
        return result > 0 ? ResultUtil.success("删除成功 " + result + " 条") : ResultUtil.error("删除失败");

    }
}